package com.tiankong.ware.controller;


import com.tiankong.ware.entity.Purchase;
import com.tiankong.ware.service.PurchaseService;
import net.youdi.common.filter.PageFilter;
import net.youdi.common.model.Page;
import net.youdi.common.model.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * <p>
 * 采购信息 前端控制器
 * </p>
 *
 * @author scl
 * @since 2021-03-22
 */
@RestController
@RequestMapping("/purchase")
public class PurchaseController {

    @Autowired
    private PurchaseService purchaseService;

    @GetMapping("list/page")
    public Result listPage(PageFilter filter){
        Page page = purchaseService.queryPage(filter);
        return Result.ok().setPage(page);
    }
    @GetMapping("info/{id}")
    public Result info(@PathVariable("id") Long id){
        Purchase purchase = purchaseService.getById(id);
        return Result.ok(purchase);
    }
    @PostMapping("save")
    public Result save(@RequestBody Purchase purchase){
        purchaseService.saveOrUpdate(purchase);
        return Result.ok(purchase);
    }
    @DeleteMapping("delete")
    public Result delete(@RequestBody Set<Long> ids){
        purchaseService.removeByIds(ids);
        return Result.ok();
    }

    /**
     * 合并采购单
     * @param purchaseId
     * @return
     */
    @PostMapping({"merge/{purchaseId}","merge"})
    public Result merge(@PathVariable(value = "purchaseId",required = false) Long purchaseId, @RequestBody Set<Long> itemIds){
        purchaseService.mergePurchase(purchaseId,itemIds);
        return Result.ok();
    }

    @PostMapping("/received")
    public Result received(@RequestBody Set<Long> ids){

        purchaseService.received(ids);

        return Result.ok();
    }

    /**
     * 完成采购单
     * @param purchase
     * @return
     */
    @PostMapping("/done")
    public Result finish(@RequestBody Purchase purchase){
        purchaseService.done(purchase);
        return Result.ok();
    }
}
